<?php

/**
 * This is the model class for table "{{user_point_history}}".
 *
 * The followings are the available columns in table '{{user_point_history}}':
 * @property integer $id
 * @property integer $user_id
 * @property integer $point
 * @property integer $action
 * @property integer $point_package_id
 */
class PointHistory extends CActiveRecord
{
	public $created_time_from, $created_time_to, $profile_id;
	
	/**
	 * Returns the static model of the specified AR class.
	 * @param string $className active record class name.
	 * @return PointHistory the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return '{{user_point_history}}';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('user_id, point, action, point_package_id, profile_id', 'numerical', 'integerOnly'=>true),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('id, user_id, point, action, point_package_id, created_time_from, created_time_to', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
			'user'=>array(self::BELONGS_TO, 'User', 'user_id'),
			'package'=>array(self::BELONGS_TO, 'PointPackage', 'point_package_id'),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'id' => 'ID',
			'user_id' => Yii::t('app', 'Nickname'),
			'point' => Yii::t('app', 'Point'),
			'action' => Yii::t('app', 'Action'),
			'point_package_id' => Yii::t('app', 'Point Package'),
			'created_time' => Yii::t('app', 'Time'),
			'price'=>Yii::t('app', 'Price'),
			'profile_id' => Yii::t('app', 'Profile ID'),
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$c=new CDbCriteria;

		if ($this->created_time_from) {
			$from = date('Y-m-d',strtotime($this->created_time_from));
			$c->addCondition("t.created_time >= '$from'");
		}

		if ($this->created_time_to) {
			$to = date('Y-m-d 23:59:59',strtotime($this->created_time_to));
			$c->addCondition("t.created_time <= '$to'");
		}

		$c->with = 'user';
		$c->compare('user.profile_id', $this->profile_id);
		$c->order = 't.created_time DESC';
		return new CActiveDataProvider($this, array(
			'criteria'=>$c,
		));
	}
}